package com.open.pokergames.biz.views.groups.fragments

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.open.pokergames.R
import com.open.pokergames.base.entity.AppPageData
import com.open.pokergames.base.entity.PageReq
import com.open.pokergames.base.fragment.EventBusRecycleViewBaseFragment
import com.open.pokergames.biz.entity.RoomVo
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.biz.views.groups.GroupActivity
import com.open.pokergames.biz.views.rooms.RoomAddActivity
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.conn.bean.WsConstant
import com.open.pokergames.texas.repository.entity.RoomEntity
import com.open.pokergames.texas.view.RoomDetailActivity
import com.open.pokergames.texas.view.adapter.RoomListAdapter
import com.open.pokergames.texas.view.cards5.Room5DetailActivity
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_fragment_page_for_group_room.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GroupRoomsFragment : EventBusRecycleViewBaseFragment<RoomVo>(), OnSuperListener<RoomEntity> {

    private var viewModel: BizViewModel? = null
    private var adapter: RoomListAdapter? = null
    private var currView: View? = null

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none;
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.comm_fragment_page_for_group_room, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = BizViewModel()
        loadingTipView.setLoadingViewBg(R.color.transparent)
//        initRecycleLinearView(loadingRecyclerView)
        initRecycleStaggeredGridView(loadingRecyclerView, 16, 2, 0)

        adapter = RoomListAdapter(activity)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")
        refreshData()

        setListener()

    }

    override fun loadData(pageVo: PageVo<RoomVo>) {
        val pageReq = PageReq<RoomVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize
        val vo = RoomVo()
        vo.groupId = GroupActivity.groupId
        pageReq.data = vo

        viewModel!!.roomsForGroup(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<RoomEntity>>) {

        if ("roomsForGroup" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常", R.drawable.data_empty)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView?.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<RoomEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.data.size == 0) {
                loadingTipView.showEmpty("俱乐部暂未开局", R.drawable.data_empty)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.data!!)
                loadingTipView?.showContent()
            }
        } else {
            if (appPageData.data.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.data!!)
                loadingTipView?.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setListener() {
        createRoomBtn.setOnClickListener {
            // 创建房间
            XUtils.intent().openActivity(activity, RoomAddActivity::class.java)
                .putStringExtra("groupId", GroupActivity.groupId)
                .requestCode(500)
                .startInFragment(this)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK && resultCode == WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK) {
            XUtils.toast().showLong("房间已经释放")
        } else {
            refreshData()
        }
    }

    override fun onClick(position: Int, itemData: RoomEntity?, flag: String?) {
        if (itemData == null) {
            XUtils.toast().show("数据异常")
            return
        }

        if (itemData.roomType == 0) {
            XUtils.intent().openActivity(activity, RoomDetailActivity::class.java)
                .putStringExtra("roomNum", itemData.roomNum)
                .putStringExtra("roomName", itemData.roomName)
                .requestCode(WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK)
                .start()
        }else if (itemData.roomType == 1){ // 5张
            XUtils.intent().openActivity(activity, Room5DetailActivity::class.java)
                .putStringExtra("roomNum", itemData.roomNum)
                .putStringExtra("roomName", itemData.roomName)
                .requestCode(WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK)
                .start()
        }
    }
}